package com.bstek.bdf4.investigate.api;

import com.bstek.bdf4.investigate.entity.Category;
import com.bstek.bdf4.investigate.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/Category")
public class CategoryController {
    @Autowired
    CategoryService categoryService ;

    @GetMapping("getTopCategories")
    public List<Category> getTopCategories() {
        Collection<Category> categories = categoryService.getTopAll();
        for (Category category : categories) {
            List<Category> childCategories = categoryService.getChildCategories(category.getId());
            if (childCategories == null || childCategories.isEmpty()) {
                category.setHasChild(false);
            } else {
                category.setHasChild(true);
            }
        }
        return (List<Category>) categories;
    }
    @GetMapping("getCategoriesById/{id}")
    public List getCategoriesById(@PathVariable Long id) {
        Collection<Category> categories = categoryService.getCategoryById(id);
        // for (Category category : categories) {
        //     List<Category> childCategories = categoryService.getChildCategories(category.getId());
        //     if (childCategories == null || childCategories.isEmpty()) {
        //         category.setHasChild(false);
        //     } else {
        //         category.setHasChild(true);
        //     }
        // }
        return (List<Category>) categories;

    }
    @GetMapping("getAllCategories")
    public List<RetuenData> getAllCategories() {
        List<RetuenData> returnDataList = new ArrayList<>();
        Collection<Category> categories = categoryService.getTopAll();
        for (Category category : categories) {
            RetuenData retuenData = new RetuenData();
            retuenData.setValue(category.getId());
            retuenData.setLabel(category.getCategoryName());
            this.findCategoriesById(category.getId(),retuenData);
            returnDataList.add(retuenData);
        }
        RetuenData retuenData = new RetuenData();
        retuenData.setLabel("顶级类别");
        retuenData.setValue(-1L);
        retuenData.setChildren(returnDataList);
        // System.out.println(returnDataList);
        List<RetuenData> list = new ArrayList<>();
        list.add(retuenData);
        return list;
    }
    public void findCategoriesById(Long id, RetuenData data) {
        Collection<Category> categoryById = categoryService.getCategoryById(id);
        List<RetuenData> returnDataList = new ArrayList<>();
        if (categoryById != null && !categoryById.isEmpty()) {
            for (Category category : categoryById) {
                RetuenData retuenData = new RetuenData();
                retuenData.setValue(category.getId());
                retuenData.setLabel(category.getCategoryName());
                returnDataList.add(retuenData);
                this.findCategoriesById(category.getId(),retuenData);
            }
            data.setChildren(returnDataList);
        }
    }



    @PostMapping("saveCategory")
    public void saveCategory(Category category) {
        if (category.getParentId() != null && category.getParentId() < 0) {
            category.setParentId(null);
        }
        if (category.getParentId() == category.getId() && category.getId() != null) {
            return;
        }
        categoryService.saveCategory(category);
    }

    @PostMapping("delCategory")
    public void delCategory(Category category) {
        if (category.getId() != null) {
            categoryService.delCategory(category.getId());
        }
    }
    class RetuenData {
        private Long value;
        private String label;
        private List<RetuenData> children;

        public Long getValue() {
            return value;
        }

        public void setValue(Long value) {
            this.value = value;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public List<RetuenData> getChildren() {
            return children;
        }

        public void setChildren(List<RetuenData> children) {
            this.children = children;
        }

        @Override
        public String toString() {
            return "RetuenData{" +
                    "value=" + value +
                    ", label='" + label + '\'' +
                    ", children=" + children +
                    '}';
        }
    }
}

